#include "ArrayDoubleParam.h"

ArrayDoubleParam::ArrayDoubleParam()
:Param() {
	values = NULL;
	size = 0;
	label = 0;
}

ArrayDoubleParam::ArrayDoubleParam( int size, char label, const char *name )
:Param( name ) {
	this->size = size;
	this->label = label;
	this->values = new SingleDoubleParam[size];

	char ename[128];
	for (int i = 0; i < size; i++) {
		sprintf(ename, "%s[%d]", name, i);
		values[i] = SingleDoubleParam(ename);
	}
	ready = true;
}

ArrayDoubleParam::ArrayDoubleParam( ArrayDoubleParam &src )
:Param( src ) {
	this->size = src.size;
	this->label = src.label;
	this->values = Util::copyArray<SingleDoubleParam>( src.values, size );
	this->ready = src.ready;
}

ArrayDoubleParam::~ArrayDoubleParam() {
	delete [] values;
}

void ArrayDoubleParam::read( Input &input ) throw (NotReady) {
	if (!ready) throwNotReady();
	for (int i = 0; i < size; i++)
		values[i].read(input);
}

SingleDoubleParam &ArrayDoubleParam::get( int i ) throw (InvalidIndex, NotReady) {
	if (!ready) throwNotReady();
	if (i < 0 || i >= size)
		throw new InvalidIndex( &label, 0, size-1, i );

	return values[i];
}

SingleDoubleParam &ArrayDoubleParam::operator [] ( int i ) throw (InvalidIndex, NotReady) {
	return get(i);
}

void ArrayDoubleParam::setSize( int size ) throw (NotReady, Exception) {
	if (!ready) throwNotReady();
	if (size <= 0)
		throw new Exception("Invalid array size");

	SingleDoubleParam *sub = new SingleDoubleParam[size];
	for (int i = 0; i < size && i < this->size; i++)
		sub[i] = values[i];

	delete [] values;
	this->values = sub;
	this->size = size;
}

void ArrayDoubleParam::setLabel( char label ) throw (NotReady) {
	if (!ready) throwNotReady();

	this->label = label;
}

int ArrayDoubleParam::getSize() throw (NotReady) {
	if (!ready) throwNotReady();

	return size;
}

char ArrayDoubleParam::getLabel() throw (NotReady) {
	if (!ready) throwNotReady();

	return label;
}